package com.asa.ui.F;

public class MineSweeperData {
	private int M;//行数
	private int N;//列数
	public boolean mine[][];//雷区
	public int number[][];//用来标记某个像素每个九宫格内炸弹的数量
	public boolean[][] open;//用来标记是否点开
	public boolean[][] flag;//用来标记是否插旗
	public MineSweeperData(int M,int N,int mineNumber){
		if(M<0||N<0){
			throw new IllegalArgumentException("传入数组大小非法");
		}
		this.M=M;
		this.N=N;
		mine=new boolean[M][N];
		number=new int[M][N];
		open=new boolean[M][N];
		flag=new boolean[M][N];
		//初始化雷区
		generateMine(M, N,mineNumber);
		//雷区计数
		countMine(M,N);
	}
	//计数
	private void countMine(int M, int N) {
		for(int i=0;i<M;i++){
			for(int j=0;j<N;j++){
				if(isMine(i, j)){
					number[i][j]=-1;
				}else{
				number[i][j]=0;
			   for(int k=i-1;k<=i+1;k++){
				   for(int q=j-1;q<=j+1;q++){
					   if(isArea(k, q)&&isMine(k,q)){
						   number[i][j]++;
					   }
				   }
			   }
			}
			}
		}
	}
	private void generateMine(int M, int N, int mineNumber) {
		//初始化雷区
		for(int i=0;i<mineNumber;i++){
			int x=i/M;
			int y=i%M;
			mine[x][y]=true;
		}
		//打乱雷区
		for(int i=M*N-1;i>=0;i--){
			int x=i/M;
			int y=i%M;
			int x1=(int)(Math.random()*i+1)/M;
			int y1=(int)(Math.random()*i+1)%M;
			swap(x,y,x1,y1);
		}
	}
	//交换
	private void swap(int x, int y, int x1, int y1) {
		boolean temp=mine[x][y];
		mine[x][y]=mine[x1][y1];
		mine[x1][y1]=temp;
	}
	//获取雷区元素
	public boolean isMine(int x,int y){
		if(!isArea(x,y)){
			throw new IllegalArgumentException("数组下标越界");
		}
		return mine[x][y];
	}
	public int getM() {
		return M;
	}
	public int getN() {
		return N;
	}
	//判断下标是否合法
	public boolean isArea(int x,int y){
		return x>=0&&y>=0&&x<M&&y<N;
	}
  
}

